<template>
  <section class="publish-article">
    <div class="article-type article-select">
      <div class="select-panel">
        <span>选择分类：</span>
        <el-select
          v-model="newArticleTypeId"
          filterable
          placeholder="请选择"
          @change="updateType"
        >
          <el-option
            v-for="item in articleType_list"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </div>
    </div>
    <div class="article-tag article-select">
      <div class="select-panel">
        <span>添加标签：</span>
        <el-select
          v-model="articleTagId"
          multiple
          filterable
          remote
          default-first-option
          placeholder="请输入关键词"
          :remote-method="remoteMethod"
          :loading="tagLoading"
          :multiple-limit="3"
          :popper-append-to-body="false"
          @change="updateTag"
        >
          <el-option
            v-for="item in tempTagList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </div>
    </div>

    <p class="btn-panel">
      <el-button type="primary" @click="$emit('publishArticle')">
        确定
      </el-button>
    </p>
  </section>
</template>

<script>
export default {
  name: 'PublishArticle',
  props: {
    articleTags: {
      type: Array,
      default: () => []
    },
    articleTypeId: {
      type: String,
      default: () => ''
    },
    articleId: {
      type: String,
      default: () => ''
    }
  },
  data() {
    return {
      newArticleTypeId: this.articleTypeId,
      articleType_list: [],
      tagLoading: false,
      remoteTagList: [],
      tempTagList: [],
      articleTagId: []
    }
  },
  created() {
    this.getTypeList()
  },
  mounted() {
    this.initTags()
  },
  methods: {
    updateTag() {
      const params = {
        articleTagId: this.articleTagId
      }
      this.updateArticleInfo(params)
    },
    updateType() {
      const params = {
        articleTypeId: this.newArticleTypeId
      }
      this.updateArticleInfo(params)
    },
    async updateArticleInfo(newInfo) {
      const params = Object.assign({
        id: this.articleId
      }, newInfo)
      const data = await this.$request({
        context: this,
        method: 'post',
        url: 'article/updateArticleInformation',
        params
      })
      if (!data) return
      this.$emit('mergeArticleData', newInfo)
    },
    async getTypeList() {
      const data = await this.$request({
        context: this,
        method: 'get',
        url: 'articleType/get'
      })
      if (!data) return
      this.articleType_list = data
    },
    initTags() {
      this.tempTagList = this.articleTags
      this.articleTags.forEach((item) => {
        this.articleTagId.push(item.id)
      })
    },
    remoteMethod(query) {
      if (query !== '') {
        this.tagLoading = true
        this.getTagList(query)
      } else {
        this.tempTagList = []
      }
    },
    async getTagList(query) {
      const params = {
        tagName: query
      }
      const data = await this.$request({
        context: this,
        method: 'get',
        url: 'tag/getLike',
        params
      })
      if (!data) return
      this.remoteTagList = data

      this.tagLoading = false
      this.tempTagList = this.remoteTagList.filter((item) => {
        return item.name.toLowerCase()
          .indexOf(query.toLowerCase()) > -1
      })
    }
  }
}
</script>

<style scoped lang="scss">
@import "~@/assets/css/public";
.publish-article {
  .btn-panel {
    display: flex;
    justify-content: flex-end;

    .el-button--primary {
      background: $theme-color;
      border: $theme-color;
    }
  }
  .select-panel {
    min-height: 38px;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .article-select {
    margin: 0 0 20px;
    /deep/ .el-select {
      flex: 1;
      .el-input__inner {
        border-color: #eaeaea;
        border-radius: 0;
        color: $theme-color;
      }
    }
  }
}
</style>
